package com.yizhang.disruptor.config;

import com.lmax.disruptor.dsl.Disruptor;
import com.yizhang.disruptor.entity.OrderEvent;
import com.yizhang.disruptor.factory.OrderEventFactory;
import com.yizhang.disruptor.handler.OrderEventHandler;
import com.yizhang.disruptor.producer.OrderEventProducer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;

/**
 * @Author: yi.zhang
 * @CreateTime: 2025-05-21 13:20:49
 * @Description: Disruptor配置
 */
@Configuration
public class DisruptorConfig {

    @Bean
    public Disruptor<OrderEvent> disruptor() {
        OrderEventFactory factory = new OrderEventFactory();
        int bufferSize = 1024;

        Disruptor<OrderEvent> disruptor = new Disruptor<>(factory, bufferSize, Executors.defaultThreadFactory());

        // 绑定事件处理器
        disruptor.handleEventsWith(new OrderEventHandler());
        disruptor.start();
        return disruptor;
    }

    @Bean
    public OrderEventProducer orderEventProducer(Disruptor<OrderEvent> disruptor) {
        return new OrderEventProducer(disruptor.getRingBuffer());
    }
}
